package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.enums.StatusEnum;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.vo.MdmStrategyMainVO;
import com.kaili.basic.pojo.vo.MdmStrategySetVO;
import com.kaili.basic.service.IStrategyMainService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.aop.OperateLogAspect;
import com.kaili.common.aop.log.enums.FunctionName;
import com.kaili.common.aop.log.enums.OperateType;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.DateUtil;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
* 策略管理 控制器
* @author:zhangjl
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "策略管理", description = "策略管理相关设置API")
public class MdmStrategyMainController {
    @Autowired
    IStrategyMainService strategyMainService;

    @GetMapping(value = "/strategymains/{id}")
    @ApiOperation("根据ID查询策略管理")
    public MdmStrategySetVO queryById(@ApiParam("ID") @PathVariable("id") Integer id) {
        return strategyMainService.queryById(id);
    }

	@GetMapping(value = "/strategymains/queryList")
	@ApiOperation("条件查询：策略管理")
	public List<MdmStrategyMainVO> queryList(@ModelAttribute MdmStrategyMainByDto dto) {
		return strategyMainService.queryList(dto);
	}

	@GetMapping(value = "/strategymains")
	@ApiOperation("分页查询策略管理")
	public Page<MdmStrategyMainVO> queryPageList(@ModelAttribute MdmStrategyMainQueryDto dto) {
		return strategyMainService.queryPageList(new Page<>(dto.getPage(),dto.getPageSize()),dto);
	}

    @PostMapping(value = "/strategymains/addSite")
    @ApiOperation("新增设备类策略Android")
    @OperateLogAspect(functionName = FunctionName.mdmStrategySet,operateType = OperateType.ADD,key = "strategyName")
    public RestStatus addSite(@RequestBody MdmStrategyMainSetAddDto dto) {
        strategyMainService.addSite(dto);

        return CommonStatus.ADD_OK;
    }

    @PostMapping(value = "/strategymains/addApply")
    @ApiOperation("新增应用类策略Android")
    @OperateLogAspect(functionName = FunctionName.mdmStrategyApply,operateType = OperateType.ADD,key = "strategyName")
    public RestStatus addApply(@RequestBody MdmStrategyMainApplyAddDto dto) {
        strategyMainService.addApply(dto);

        return CommonStatus.ADD_OK;
    }

    @PostMapping(value = "/strategymains/addUpgrade")
    @ApiOperation("新增升级类策略")
    @OperateLogAspect(functionName = FunctionName.mdmStrategyUpgrade,operateType = OperateType.ADD,key = "strategyName")
    public RestStatus addUpgrade(@RequestBody MdmStrategyMainUpgradeAddDto dto) {
        strategyMainService.addUpgrade(dto);

        return CommonStatus.ADD_OK;
    }

    @PutMapping(value = "/strategymains/updateSite")
    @ApiOperation("修改设备类策略Android")
    @OperateLogAspect(functionName = FunctionName.mdmStrategySet,operateType = OperateType.UPDATE,key = "strategyName")
    public RestStatus updateSite(@RequestBody MdmStrategyMainSetUpdateDto dto) {
        strategyMainService.modify(dto);

        return CommonStatus.UPDATE_OK;
    }

    @PutMapping(value = "/strategymains/updateApply")
    @ApiOperation("修改应用类策略Android")
    @OperateLogAspect(functionName = FunctionName.mdmStrategyApply,operateType = OperateType.UPDATE,key = "strategyName")
    public RestStatus updateApply(@RequestBody MdmStrategyMainApplyUpdateDto dto) {
        strategyMainService.modifyApply(dto);

        return CommonStatus.UPDATE_OK;
    }

    @PutMapping(value = "/strategymains/updateUpgrade")
    @ApiOperation("修改升级类策略")
    @OperateLogAspect(functionName = FunctionName.mdmStrategyUpgrade,operateType = OperateType.UPDATE,key = "strategyName")
    public RestStatus updateUpgrade(@RequestBody MdmStrategyMainUpgradeupdateDto dto) {
        strategyMainService.modifyUpgrade(dto);

        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping(value = "/strategymains/{id}")
    @ApiOperation("删除策略管理")
    @OperateLogAspect(functionName = FunctionName.mdmStrategySet,operateType = OperateType.DELETE)
    public RestStatus del(@ApiParam("策略管理主ID") @PathVariable("id") Integer id) {
        strategyMainService.updateStatus(Arrays.asList(id), StatusEnum.DEL.getKey());

        return CommonStatus.DELETE_OK;
    }

    @GetMapping(value = "/strategymains/export")
    @ApiOperation("策略管理导出")
    public void export(HttpServletResponse response, @ModelAttribute MdmStrategyMainQueryDto dto) throws RestException {
        List<MdmStrategyMainVO> list = strategyMainService.queryExportList(dto);
        ExcelUtil.exportExcel(list,"策略管理","sheet1",MdmStrategyMainVO.class, "策略管理"+DateUtil.getNowDate() +".xls",response);
    }

    @GetMapping(value = "/strategymains/querySet")
    @ApiOperation("PDA策略设备接口")
    public List<MdmStrategySetVO> querySet(@ModelAttribute MdmStrategyMainByDto dto) {
        return strategyMainService.querySet(dto);
    }

}
